package com.tjb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import com.tjb.reggie.common.R;
import com.tjb.reggie.entity.Employee;
import com.tjb.reggie.service.EmployeeService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author tjb
 * @version 1.0
 * @date 2022/8/24 18:43
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee : 员工表单
     * @param request : 请求
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){
        /**
         * 1、将页面提交过来的用户名username去查询数据库
         * 2、如果没有查到用户就返回
         * 3、根据查询出来的密码和传入的用户密码（进行md5加密），进行比较
         * 4、查看员工状态，如果是禁用状态，则返回员工禁用
         * 5、登录成功，将员工的id存入session
         */
        String username = employee.getUsername();
        String password = employee.getPassword();
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Employee one = employeeService.getOne(wrapper);
        if (one == null){
            R.error("用户不存在");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(one.getPassword())){
            R.error("密码错误");
        }
        if (one.getStatus() == 0){
            R.error("用户已经被禁用");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request){
        //清除session中的值
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    /**
     * 新增员工
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        //根据名字进行查询是否存在，如果存在就返回信息
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getName, employee.getName());
        employeeService.getOne(wrapper);
        //设置初始密码，使用md5进行加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增成功");
    }

    /**
     * 员工信息的分页查询
     * @param page : 第几页
     * @param pageSize ： 页面大小
     * @param name ： 姓名
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String name){
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(name), Employee::getName, name);
        wrapper.orderByDesc(Employee::getCreateTime);
        employeeService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request){
        employeeService.updateById(employee);
        return R.success("更新成功");
    }
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("查无此人");
    }
}
